import os
from time import sleep
from selenium.webdriver.common.by import By
from page.mainpage import MainPage
from common.readpath import ReadPath


class StudentManagement(MainPage):

    def goto_studentmanagemant(self):
        """
        进入侧导航栏排序第一的县级的学校管理
        打开第一所学校管理菜单
        进入该学校的学生管理
        :return:
        """
        self.click("css", '#app > div > section > div > div:nth-child(2) > section > ul > li:nth-child(1)')
        self.click("css",
                   '#app > div > section > div > div:nth-child(2) > section > ul > li.secondstage.el-submenu.is-active.is-opened > ul > li:nth-child(1)')
        self.click("xp", "//*[text()='学生管理']")
        sleep(1)

    def loacate_addstudent(self):
        """
        定位新增学生按钮
        :return:
        """
        self.locate("css","div.main-content > section > section > div.options > div.actionsgroup > button:nth-child(3)")

    def add_student(self, student_name, class_name, parent_contact, school_num, password, student_ID):
        """
        新增学生
        输入:param
        点击提交
        :param student_name:学生姓名
        :param class_name: 所在班级
        :param parent_contact:家长联系方式
        :param school_num: 学号
        :param password: 密码
        :param student_ID: 学生ID
        :return:
        """
        self.click("css", "div.main-content > section > section > div.options > div.actionsgroup > button:nth-child(3)")
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(1) > div > input",
                       student_name)
        if class_name == "":
            pass
        else:
            self.click("css", "div.main-content > section > section > section > div:nth-child(2) > div > div > input")
            self.click("xp", f"//*[text()='{class_name}']")
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(3) > div > input",
                       parent_contact)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(4) > div > input",
                       school_num)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(5) > div > input",
                       password)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(6) > div > input",
                       student_ID)
        self.click_submit()

    def edit_student(self, student_name, class_name, parent_contact, school_num, password, student_ID):
        """
        选择学生列表第一位学生编辑
        输入param
        点击提交
        :param student_name:学生姓名
        :param class_name: 所在班级
        :param parent_contact:家长联系方式
        :param school_num: 学号
        :param password: 密码
        :param student_ID: 学生ID
        :return:
        """
        self.click("css",
                   "div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child(1) > td.el-table_1_column_10.is-center > div > button:nth-child(1)")
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(1) > div > input",
                       student_name)
        self.click("css", "div.main-content > section > section > section > div:nth-child(2) > div > div > input")
        self.click("xp", f"//*[text()='{class_name}']")
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(3) > div > input",
                       parent_contact)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(4) > div > input",
                       school_num)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(5) > div > input",
                       password)
        self.send_keys("css", "div.main-content > section > section > section > div:nth-child(6) > div > input",
                       student_ID)
        self.click_submit()

    def delete_num1_student(self):
        """
        点击删除学生列表第一位学生
        :return:
        """
        self.click("css",
                   "div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child(1) > td.el-table_1_column_10.is-center > div > button:nth-child(2)")

    def goto_num1_studentdetails(self):
        """
        进入学生列表第一位学生信息详情
        :return:
        """
        self.click("css",
                   "div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child(1) > td.el-table_1_column_10.is-center > div > button:nth-child(3)")

    def backdetails(self):
        """
        从学生详情返回学生列表
        :return:
        """
        self.click("css","div.main-content > section > section > section > div.backbtn > span.return > i")

    def get_num1_studentname(self):
        """

        :return:获取排在学生列表第一个学生的姓名
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(1) > td.el-table_1_column_1.is-center")

    def get_num1_schoolnum(self):
        """

        :return: 获取排在学生列表第一个学生的学号
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(1) > td.el-table_1_column_4.is-center")

    def get_num2_studentname(self):
        """

        :return:获取排在学生列表第二个学生的姓名
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(2) > td.el-table_1_column_1.is-center")

    def import_student(self, class_name, file):
        """
        导入学生
        :param file:需要上传的文件名
        :return:
        """
        self.click("css", "div.options > div.actionsgroup > button:nth-child(4)")
        if class_name == "":
            file_path = os.path.join(ReadPath().get_datafile_path(), file)
            # 上传文件(此处上传文件,不能用封装的.send_keys,因为里面包含其他的方法)
            self.driver.find_element(By.NAME, "file").send_keys(file_path)
            sleep(1)
            self.close_popup()
        else:
            self.click("css", "div.el-dialog__body > div > div.el-select > div > input")
            self.click("xp", f"//*[text()='{class_name}']")
            file_path = os.path.join(ReadPath().get_datafile_path(), file)
            # 上传文件(此处上传文件,不能用封装的.send_keys,因为里面包含其他的方法)
            self.driver.find_element(By.NAME, "file").send_keys(file_path)
            sleep(1)
            self.close_popup()
        self.click_importpopup_submit()

    def upload_grade(self, class_name, file):
        """
        上传成绩
        :param file:需要上传的文件名
        :return:
        """
        self.click("css", "div.options > div.actionsgroup > button:nth-child(5)")
        if class_name == "":
            file_path = os.path.join(ReadPath().get_datafile_path(), file)
            # 上传照片(此处上传照片,不能用封装的.send_keys,因为里面包含其他的方法)
            self.driver.find_element(By.NAME, "file").send_keys(file_path)
            sleep(1)
            self.close_popup()
        else:
            self.click("css", "div.el-dialog__body > div > div.el-select > div > input")
            self.click("xp", f"//*[text()='{class_name}']")
            file_path = os.path.join(ReadPath().get_datafile_path(), file)
            # 上传照片(此处上传照片,不能用封装的.send_keys,因为里面包含其他的方法)
            self.driver.find_element(By.NAME, "file").send_keys(file_path)
            sleep(1)
            self.close_popup()
        self.click_importpopup_submit()